<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-22
 * Time: 23:19
 */

namespace app\api\service\withdrawal;


use app\api\model\Assistant;
use app\api\model\AssistantMoney;
use app\api\model\CityPartner;
use app\api\model\CityPartnerMoney;
use app\api\model\CountyPartner;
use app\api\model\CountyPartnerMoney;
use app\api\model\ProvincePartner;
use app\api\model\ProvincePartnerMoney;
use app\api\model\User;
use app\api\model\UserInvitationMoney;
use app\api\model\Withdrawal;
use LinCmsTp5\exception\ParameterException;

class WithdrawalSer
{
    /**
     * 1.根据类型判断当前的信息
     * 2.查询正在提现的金额
     * 3.当前金额和所有金额进行对比
     * 4.大于可以提现，小于进行提示
     */

    // 信息ID
    private $id;

    // 创建的信息
    private $postArr;

    // 查询信息
    private $info;

    function __construct($id)
    {
        $this->id = $id;
    }

    // 入口方法
    public function entrance()
    {
        $this->postArr = Withdrawal::get($this->id);
        return $this->judgeData();
    }

    // 判断数据
    private function judgeData()
    {
        if($this->postArr['status'] == 2) {
            throw new ParameterException([
                'msg' => [
                    'money' => '提现已审核通过'
                ]
            ]);
        }
        if($this->postArr['type'] == 1) {
            $this->info = User::get($this->postArr['pid']);
            if($this->info['invite_money'] <= 0) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '余额不足'
                    ]
                ]);
            }
            $money = Withdrawal::where('pid',$this->postArr['pid'])
                ->where('status',1)
                ->where('type',$this->postArr['type'])
                ->sum('money');
            if($this->info['invite_money'] < $money) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '余额不足'
                    ]
                ]);
            }
            $Withdrawal = new Withdrawal();
            $Withdrawal->save([ 'status'=>2 ],[ 'id' => $this->id]);
            UserInvitationMoney::create([
                'user_id' => $this->info['id'],
                'money' => $this->postArr['money'],
                'status' => 2,
                'user_invite_money' => $this->info['invite_money']
            ]);
            User::where('id',$this->postArr['pid'])->setDec('invite_money',$this->postArr['money']);
        } elseif($this->postArr['type'] == 2) {
            $this->info = ProvincePartner::where('id',$this->postArr['pid'])
                ->where('status',2)->find();
            if(!$this->info) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '还不是省合伙人'
                    ]
                ]);
            }
            if($this->info['money'] <= 0) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '余额不足'
                    ]
                ]);
            }
            $money = Withdrawal::where('pid',$this->info['id'])
                ->where('status',1)
                ->where('type',$this->postArr['type'])
                ->sum('money');
            if($this->info['money'] < $money) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '余额不足'
                    ]
                ]);
            }
            $Withdrawal = new Withdrawal();
            $Withdrawal->save([ 'status'=>2 ],[ 'id' => $this->id]);
            ProvincePartnerMoney::create([
                'province_partner_id' => $this->info['id'],
                'money' => $this->postArr['money'],
                'status' => 2,
                'province_partner_money' => $this->info['money']
            ]);
            ProvincePartner::where('id',$this->postArr['pid'])->setDec('money',$this->postArr['money']);
        } elseif($this->postArr['type'] == 3) {
            $this->info = CityPartner::where('id',$this->postArr['pid'])->where('status',2)->find();
            if(!$this->info) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '还不是市合伙人'
                    ]
                ]);
            }
            if($this->info['money'] <= 0) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '余额不足'
                    ]
                ]);
            }
            $money = Withdrawal::where('pid',$this->info['id'])
                ->where('type',$this->postArr['type'])
                ->where('status',1)
                ->sum('money');
            if($this->info['money'] < $money) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '余额不足'
                    ]
                ]);
            }
            $Withdrawal = new Withdrawal();
            $Withdrawal->save([ 'status'=>2 ],[ 'id' => $this->id]);
            CityPartnerMoney::create([
                'city_partner_id' => $this->info['id'],
                'money' => $this->postArr['money'],
                'status' => 2,
                'city_partner_money' => $this->info['money']
            ]);
            CityPartner::where('id',$this->postArr['pid'])->setDec('money',$this->postArr['money']);
        } elseif($this->postArr['type'] == 4) {
            $this->info = CountyPartner::where('id',$this->postArr['pid'])
                ->where('status',2)
                ->find();
            if(!$this->info) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '还不是区/县合伙人'
                    ]
                ]);
            }
            if($this->info['money'] <= 0) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '余额不足'
                    ]
                ]);
            }
            $money = Withdrawal::where('pid',$this->info['id'])
                ->where('type',$this->postArr['type'])
                ->where('status',1)
                ->sum('money');
            if($this->info['money'] < $money) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '余额不足'
                    ]
                ]);
            }
            $Withdrawal = new Withdrawal();
            $Withdrawal->save([ 'status'=>2 ],[ 'id' => $this->id]);
            CountyPartnerMoney::create([
                'county_partner_id' => $this->info['id'],
                'money' => $this->postArr['money'],
                'status' => 2,
                'county_partner_money' => $this->info['money']
            ]);
            CountyPartner::where('id',$this->postArr['pid'])->setDec('money',$this->postArr['money']);
        }  elseif($this->postArr['type'] == 5) {
            $this->info = Assistant::where('id',$this->postArr['pid'])->find();
            if(!$this->info) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '还不是助理'
                    ]
                ]);
            }
            if($this->info['money'] <= 0) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '余额不足'
                    ]
                ]);
            }
            $money = Withdrawal::where('pid',$this->info['id'])
                ->where('type',$this->postArr['type'])
                ->where('status',1)
                ->sum('money');
            if($this->info['money'] < $money) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '余额不足'
                    ]
                ]);
            }
            AssistantMoney::create([
                'assistant_id' => $this->info['id'],
                'money' => $this->postArr['money'],
                'status' => 2,
                'assistant_money' => $this->info['money']
            ]);
            Assistant::where('id',$this->postArr['pid'])->setDec('money',$this->postArr['money']);
        }
        return writeJson(201, [], '操作成功！');
    }

}